package cn.tedu.egweibo05.controller;

import cn.tedu.egweibo05.common.response.JsonResult;
import cn.tedu.egweibo05.mapper.CommentMapper;
import cn.tedu.egweibo05.mapper.UserMapper;
import cn.tedu.egweibo05.pojo.dto.CommentDTO;
import cn.tedu.egweibo05.pojo.entity.Comment;
import cn.tedu.egweibo05.pojo.vo.CommentVO;
import cn.tedu.egweibo05.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发表评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        /*
            1.校验登录状态;
            2.根据登录状态执行业务逻辑.
         */
        UserVO userVO = (UserVO) session.getAttribute("user");

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);

        return JsonResult.ok();
    }
    @ApiOperation("评论列表")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Long id){
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);

        return JsonResult.ok(commentVOS);
    }
}
